package com.play.ballen.me.vm

import com.play.ballen.R
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.play.ballen.utils.UserInfoManager
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil
import io.reactivex.Observable
import java.util.concurrent.TimeUnit

class ForgetPwdVM : BaseViewModel() {

    private val model = ApiRepository()

    val actionResetSuccess = SingleLiveEvent<Void>()
    val actionCountDownComplete = SingleLiveEvent<Void>()
    val actionCountDown = SingleLiveEvent<Long>()

    private val timeCountDown = 59L

    fun sendCode(name: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        val map = mutableMapOf<String, Any>()
        map["name"] = name
        map["type"] = "forget"
        model.sendCode(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                startCountDown()
                Logger.i("ForgetPwdVM.sendCode success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("ForgetPwdVM.sendCode error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    private fun startCountDown() {
        Observable.interval(0, 1, TimeUnit.SECONDS)
            .take(timeCountDown + 1)
            .map { timeCountDown - it }
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                actionCountDownComplete.call()
            }
            .subscribe({
                actionCountDown.value = it
            }, { throwable ->
                throwable.printStackTrace()
                Logger.i("ForgetPwdVM.startCountDown error,msg = ${throwable.message}")
            }, {
            }).addToDisposable(compositeDisposable)
    }

    fun forgetPwd(type: Int, account: String, password: String, code: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        val map = mutableMapOf<String, Any>()
        map["type"] = type
        map["name"] = account
        map["password"] = password
        map["code"] = code
        model.forgetPwd(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("ForgetPwdVM.forgetPwd success")
                actionResetSuccess.call()
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("ForgetPwdVM.forgetPwd error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }
}